
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include <project_config.h>

using namespace std;

std::vector<std::string> split(const std::string& str, char delimiter) {
    std::vector<std::string> tokens;
    std::istringstream iss(str);
    std::string token;
    while (std::getline(iss, token, delimiter)) {
        tokens.push_back(token);
    }
    return tokens;
}

std::string trim(const std::string& str) {
    size_t first = str.find_first_not_of(" \t\r\n");
    size_t last = str.find_last_not_of(" \t\r\n");

    if (first != std::string::npos && last != std::string::npos) {
        return str.substr(first, last - first + 1);
    }

    return "";
}

std::string get_file_content(std::string filepath) {
    std::ifstream file(filepath);
    if (!file.is_open()) {
        throw std::runtime_error("file opened failed.");
    }
    std::string line;
    std::stringstream ss; 
    while (std::getline(file , line))
    {
        ss << line;
        ss << "\n";
    }
    file.close();
    return ss.str();
}

bool startsWith(const std::string& str, const std::string& prefix) {
    if (prefix.length() > str.length()) {
        return false;
    }
    return std::equal(prefix.begin(), prefix.end(), str.begin());
}


void write_to_file(string filename , string file_content , string mod) {
    FILE *myfile = fopen(filename.c_str(), mod.c_str());
    if (myfile != nullptr) {
        fprintf(myfile, file_content.c_str());
        fclose(myfile);
    } else {
        throw runtime_error("can not open file.");
    }
}